Break and Continue in Javascript
Published on: July 03, 2021 By T.Andrew Rayan
Break Statement
The break statement is used to break out of a running loop. This will stop the further execution of the loop even if the condition is true to continue the loop.
The break statement can be helpful to stop unwanted execution of the loop and save the execution time of a program.
Example for break statement
In the above program we have a function checkNumber to get array of numbers and a number variable to check whether the number is present in the array or not.
Here if the number is available in the array then it will set the isAvailable variable to true and breaks from the loop using the break statement. So here further 5 checks are skipped.
Continue statement
The continue statements are used to break one iteration of the loop and continues the rest of the iterations.
The continue statement can be useful in the scenarios when we no need to process the statements for certain condition. Let's see with an example.
Example for continue statement
The above program will print the numbers from 1 to 10 to the console, skipping when the value of i is 5. When the value of i becomes 5 then the statements belong the continue statements gets skipped for that execution and continues to process the next iteration in the loop.